int pulsePin = PIN_D0;
int latchPin = PIN_D1;
int dataPin = PIN_D2;
int lr = 0;
int ud = 0;
void setup(){
  pinMode(pulsePin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin,  INPUT);
}
void loop(){
  ud=0;
  lr=0;
  latch();
  delay(6);
  if(!digitalRead(dataPin))
    Keyboard.set_key1(KEY_X);
  else
    Keyboard.set_key1(0);
  pulse();
  if(!digitalRead(dataPin))
      Keyboard.set_key2(KEY_Z);
  else
    Keyboard.set_key2(0);
  pulse();
  if(!digitalRead(dataPin))
      Keyboard.set_key3(KEY_S);
  else
      Keyboard.set_key3(0);
  pulse();
  if(!digitalRead(dataPin))
      Keyboard.set_key4(KEY_ENTER);
  else
      Keyboard.set_key4(0);
  pulse();
  if(!digitalRead(dataPin))
      ud=1;
  pulse();
  if(!digitalRead(dataPin))
      ud=2;
  pulse();
  if(!digitalRead(dataPin))
      lr=1;
  pulse();
  if(!digitalRead(dataPin))
      lr=2;
  if (lr > 0){
    if (lr > 1){
      Keyboard.set_key6(KEY_RIGHT);
    }
    else
      Keyboard.set_key6(KEY_LEFT);
  }
  else
    Keyboard.set_key6(0);
  if (ud > 0){
    if (ud > 1){
      Keyboard.set_key5(KEY_DOWN);
    }
    else
      Keyboard.set_key5(KEY_UP);
  }
  else
    Keyboard.set_key5(0);
    
  Keyboard.send_now();
}
void pulse(){
  delay(6);
  digitalWrite(pulsePin, HIGH);
  delay(6);
  digitalWrite(pulsePin, LOW);  
}
void latch(){
   digitalWrite(latchPin, HIGH);
   delay(12);
   digitalWrite(latchPin, LOW);
}
  
